package com.bangbang.controller;


import com.alibaba.fastjson.JSONObject;
import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.mapper.TbTeacherMapper;
import com.bangbang.pojo.TbStudentUser;
import com.bangbang.pojo.TbTeacher;
import com.bangbang.util.IdWorker;
import com.bangbang.util.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.UUID;

/**
 * <p>
 * 教室信息表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-13
 */
@RestController
@RequestMapping("tbTeacher")
public class TbTeacherController {


    @Autowired
    private TbTeacherMapper tbStudentUser;

    @Autowired
    private RedisTemplate redisTemplate;


    // 教师端注册
    @ApiOperation(value = "教师端注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbStudentUser",value = "教师表的实体类",required = true,paramType = "body"),
    })
    @PostMapping
    public Result studentRegister (@RequestBody TbTeacher tbTeacher){
        if ("".equals(tbTeacher.getTeaPassword())){
            return Result.buildResult(ResultEnum.ERROR).setResult("不能为空");
        }
        String salt = ShiroUtils.generateSalt(6);
        String password = tbTeacher.getTeaPassword();
        tbTeacher.setTeaPassword(ShiroUtils.encryptPassword("MD5", password, salt, 3));
        IdWorker idWorker = new IdWorker();
        long teacherAccount = idWorker.nextId();
        String account = String.valueOf(teacherAccount);
        tbTeacher.setTeaAccount(account).setTeaSalt(salt);
        tbStudentUser.insert(tbTeacher);
        return Result.buildResult(ResultEnum.SUCCESS);
    }


//    教师登录
        @GetMapping
        @ApiOperation(value = "教师登录")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "teacherAccount",value = "教师账号账号",required = true,paramType = "query"),
                @ApiImplicitParam(name = "teacherPwd",value = "教师密码",required = true,paramType = "query"),
        })
        public Result Login(@RequestParam(required = true) String teacherAccount,@RequestParam(required = true) String teacherPwd){
            Subject subject = SecurityUtils.getSubject();
            String user = "2";
            UsernamePasswordToken token = new UsernamePasswordToken(teacherAccount,teacherPwd,user);
        //        account  获取到登录用户的对象
            QueryWrapper<TbTeacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.eq("tea_account", teacherAccount);
            TbTeacher tbTeacher = tbStudentUser.selectOne(teacherQueryWrapper);
            String NowUser= UUID.randomUUID().toString();
            NowUser.replace("-","");
            //String s = JSONObject.toJSONString(tbTeacher);
           // redisTemplate.boundHashOps(NowUser).put("teacher",s);

            redisTemplate.boundValueOps(NowUser).set(tbTeacher.getTeaId());
            return Result.buildResult(ResultEnum.SUCCESS).setResult(NowUser);//把这个uuid给前台,拿去判断
    }

}

